package com.aionemu.gameserver.controllers.movement;

import com.aionemu.gameserver.configs.administration.AdminConfig;
import com.aionemu.gameserver.model.flyring.FlyRing;
import com.aionemu.gameserver.model.gameobjects.player.Player;
import com.aionemu.gameserver.model.templates.utils3d.Point3D;
import com.aionemu.gameserver.network.aion.serverpackets.SM_ATTACK_STATUS;
import com.aionemu.gameserver.network.aion.serverpackets.SM_ATTACK_STATUS.TYPE;
import com.aionemu.gameserver.utils.MathUtil;
import com.aionemu.gameserver.utils.PacketSendUtility;

import java.util.Random;


public class FlyRingObserver extends ActionObserver {
    private Player player;
    private FlyRing ring;
    private Point3D oldPosition;
    private Random random;

    public FlyRingObserver() {
        super(ObserverType.MOVE);
        this.player = null;
        this.ring = null;
        this.oldPosition = null;
        this.random = null;
    }

    public FlyRingObserver(FlyRing ring, Player player) {
        super(ObserverType.MOVE);
        this.player = player;
        this.ring = ring;
        this.oldPosition = new Point3D(player.getX(), player.getY(), player.getZ());
        this.random = new Random();
    }

    @Override
    public void moved() {
        Point3D newPosition = new Point3D(player.getX(), player.getY(), player.getZ());
        boolean passedThrough = false;

        if (ring.getPlane().intersect(oldPosition, newPosition)) {
            Point3D intersectionPoint = ring.getPlane().intersection(oldPosition, newPosition);
            if (intersectionPoint != null) {
                double distance = Math.abs(ring.getPlane().getCenter().distance(intersectionPoint));

                if (distance < ring.getTemplate().getRadius()) {
                    passedThrough = true;
                }
            } else {
                if (MathUtil.isIn3dRange(ring, player, ring.getTemplate().getRadius())) ;
                {
                    passedThrough = true;
                }
            }
        }

        if (passedThrough) {
            if (player.getAccessLevel() >= AdminConfig.COMMAND_RING) {
                PacketSendUtility.sendMessage(player, "You passed through fly ring " + ring.getName());
            }

            int fpBonus = 7 + random.nextInt(7);
            if (player.getLifeStats().getCurrentFp() + fpBonus > player.getLifeStats().getMaxFp()) {
                fpBonus = player.getLifeStats().getMaxFp() - player.getLifeStats().getCurrentFp();
            }
            if (fpBonus > 0) {
                PacketSendUtility.sendPacket(player, new SM_ATTACK_STATUS(player, TYPE.FP_RINGS, 0, fpBonus));
                player.getLifeStats().increaseFp(fpBonus);
            }
        }

        oldPosition = newPosition;
    }
}
